package com.cyandpos.pad.cateringformat.choosemenu;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cyandpos.pad.R;
import com.cyandpos.pad.posdata.CyFood;
import com.squareup.picasso.Picasso;

import java.util.List;

public class FoodAdapter extends BaseAdapter {
    private List<CyFood.CyGood> list;
    private Context context;

    public FoodAdapter(List<CyFood.CyGood> list, Context context){
        this.list=list;
        this.context=context;
    }

    @Override
    public int getCount() {
        if(list!=null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public CyFood.CyGood getItem(int position) {
        if(list!=null) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView==null){
            holder=new Holder();
            convertView= LayoutInflater.from(context).inflate(R.layout.item_food,null);
            holder.iv_food=convertView.findViewById(R.id.iv_food);
            holder.tv_food_name=convertView.findViewById(R.id.tv_food_name);
            convertView.setTag(holder);
        }else{
            holder= (Holder) convertView.getTag();
        }

        CyFood.CyGood good=getItem(position);
        if(good!=null){
            holder.tv_food_name.setText(good.getName()+"  ￥"+good.getPrice());
            if(good.getPictureUrl()!=null&&!"".equals(good.getPictureUrl())) {
                Picasso.get().load(good.getPictureUrl()).into(holder.iv_food);
            }
        }
        return convertView;
    }

    class Holder{
        private ImageView iv_food;
        private TextView tv_food_name;
    }
}
